/********************************************************************
 * Copyright(c) 2023-2023 Grayson Zheng.
 * File: uart.C
 * Description: The header file of the LCD1602 driver for AT89C52.
 * Version: 1.1
 * Date: 2024-01-04
 * Author: zhengxinyu13@qq.com
 * ---------- Revision History ----------
 * <version>
 * <date>
 * <author>
 * <desc>
 * Modified to be suitable to the new coding rules in all functions.
 ********************************************************************/

#include "regx52.h"
#include "datatype.h"
#include "bit.h"

#ifndef __LCD1602_H__
#define __LCD1602_H__

#define CLEAR_DISPLAY	0x01
#define CURSOR_HOME		0x02
#define ENTRY_MODE_SET	0x04
#define DISPLAY_CONTROL	0x08
#define CURSOR_SHIFT	0x10
#define FUNCTION_SET	0x20
#define CGRAM_ADDR		0x40
#define DDRAM_ADDR		0x80

#ifdef ENTRY_MODE_SET

#define DEC_CURSOR			0x00  // decrement cursor position.
#define INC_CURSOR			0x02  // increment cursor position.
#define NO_DISPLAY_SHIFT	0x00
#define DISPLAY_SHIFT		0x01

#endif

#ifdef DISPLAY_CONTROL

#define DISPLAY_OFF			0x00
#define DISPLAY_ON			0x04
#define CURSOR_OFF			0x00
#define CURSOR_ON			0x02
#define CURSOR_BLINK_OFF	0x00
#define CURSOR_BLINK_ON		0x01

#endif

#ifdef CURSOR_SHIFT

#define MOVE_CURSOR		0x00
#define SHIFT_DISPLAY	0x08
#define SHIFT_LEFT		0x00
#define SHIFT_RIGHT		0x04

#endif

#ifdef FUNCTION_SET

#define _4_BIT_INTERFACE_	0x00
#define _8_BIT_INTERFACE_	0x10
#define _8_BY_1_OR_11_BY_1_	0x00
#define _16_BY_2_			0x08
#define _5_BY_8_DOTS_		0x00
#define _5_BY_10_DOTS_		0x04

#endif

#define LCD_DATA P0

sbit LCD_BUSY = P0^7;  // When LCD1602 is in a busy state, this bit is high.

sbit LCD_RS = P2^7;  // Register Select:
					 // When RS=1, the communication is data;
					 // When RS=0, the communication is commands.
sbit LCD_RW = P2^6;  // Read/Write Select:
					 // When R/W=1, it indicates reading data from the LCD;
					 // when R/W=0, it indicates writing data to the LCD.
sbit LCD_EN = P2^5;  // Enable signal:
					 // When E=1, it means reading information;
					 // When E changes from 1 to 0, the LCD module begins to execute commands.

typedef enum line {
	ROW_0,
	ROW_1,
}LINE;

void lcd1602_busy_detection(void);
void lcd1602_write_command(uint_8 command);
void lcd1602_write_data(uint_8 dat);
void lcd1602_init(void);
void lcd1602_clear_screen(void);
void lcd1602_shift_left(void);
void lcd1602_shift_right(void);
void lcd1602_set_cursor(LINE row, uint_8 column);
void lcd1602_show_char(LINE row, uint_8 column, uint_8 character);
void lcd1602_show_string(LINE row, uint_8 column, uint_8 *string);
void lcd1602_show_number(LINE row, uint_8 column, int_16 number);
void lcd1602_show_hex(LINE row, uint_8 column, uint_16 number);
void lcd1602_show_bin(LINE row, uint_8 column, uint_16 number);

#endif //__LCD1602_H__
